package com.hyt.it.ogt.kq.service.gov.schedule.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import com.hyt.it.ogt.kq.service.gov.schedule.model.ScheduleResult;
import com.hyt.it.ogt.kq.service.gov.schedule.model.Candidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ExamInfo;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ScheduleExamRoom;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ScheduleExamRoom.ScheduleCandidate;

public class ScheduleUtils {

    /***
     * 得到编排任务的所有考生
     * 
     * @param examInfo
     * @return
     */
    public static List<Candidate> getExamCandidates(ExamInfo examInfo) {
        return examInfo.getCandidates();
    }

    /****
     * 得到编排任务按批次，考生ID的考生信息的MAP
     * 
     * @param examInfos
     * @return
     */
    public static Map<String, Map<String, Candidate>> getExamCandidateMap(List<ExamInfo> examInfos) {
        Map<String, Map<String, Candidate>> totalExamCandidateMap = new HashMap<>();
        for (ExamInfo examInfo : examInfos) {
            totalExamCandidateMap.put(examInfo.getBatchId(), getExamCandidateMap(examInfo));
        }
        return totalExamCandidateMap;
    }

    public static Map<String, Candidate> getExamCandidateMap(ExamInfo examInfo) {
        List<Candidate> candidates = getExamCandidates(examInfo);
        Map<String, Candidate> candidateMap = candidates.stream()
                .collect(Collectors.toMap(Candidate::getBmStudentId, item -> item));
        return candidateMap;
    }

    /**
     * 得到编排后的已编排考生信息
     * 
     * @return
     */
    public static List<ScheduleCandidate> getScheduledCandidates(ScheduleResult scheduleResult) {
        List<ScheduleCandidate> scheduleCandidates = new ArrayList<>();
        for (ScheduleExamRoom examRoom : scheduleResult.getExamRooms()) {
            scheduleCandidates.addAll(examRoom.getCandidates());
        }
        return scheduleCandidates;
    }

    public static List<String> batchGetCandidateIds(Set<Candidate> candidates,
            Map<String, Map<String, String>> virId2CandidateIdMap) {
        Set<String> candidateVirIds = new HashSet<>();
        Map<String, Set<String>> candidateId2VirIdMap = new HashMap<>();
        for (Map.Entry<String, Map<String, String>> entry : virId2CandidateIdMap.entrySet()) {
            String virCandidateId = entry.getKey();
            for (String candidateId : entry.getValue().values()) {
                if (!candidateId2VirIdMap.containsKey(candidateId)) {
                    candidateId2VirIdMap.put(candidateId, new HashSet<>());
                }
                candidateId2VirIdMap.get(candidateId).add(virCandidateId);
            }
        }
        for (Candidate candidate : candidates) {
            candidateVirIds.addAll(candidateId2VirIdMap.get(candidate.getBmStudentId()));
        }
        return new ArrayList<>(candidateVirIds);
    }
}
